import {
  getCourseSchedule,
  searchCourse,
  selectCourse,
  dropCourse,
  getScore,
  addComment,
  getAllScore,
  updateScore,
  updateCourse,
  deleteCourse,
  addCourse,
  updateTeacher
} from '@/api/course'
import type { Course, Score, GPA } from '@/models/course'
import { toSeperatedCourse, toMergedCourse } from '@/utils/courseUtil'
export async function GetCourseSchedule(): Promise<Course[]> {
  const res = (await getCourseSchedule()) as any
  return toSeperatedCourse(res.data)
}
export async function GetMergedCourseSchedule(): Promise<Course[]> {
  const res = (await getCourseSchedule()) as any
  return toMergedCourse(res.data)
}
export async function GetMergedCourseName(): Promise<string[]> {
  const nameList: string[] = []
  const res = (await GetMergedCourseSchedule()) as Course[]
  res.forEach((item: Course) => {
    const a: string = item.name
    nameList.push(a)
  })
  return nameList
}
export async function GetMergedCourseId(): Promise<{ goodList: number[], normalList: number[], badList: number[] }> {
    const idList: number[] = [];
    const res = (await GetMergedCourseSchedule()) as Course[];

    for (const item of res) {
        const a: number = item.id;
        idList.push(a);
    }

    const goodList: number[] = [], normalList: number[] = [], badList: number[] = [];

    for (const id of idList) {
        const data = await GetCourseScore(id);
        goodList.push(data.good);
        normalList.push(data.normal);
        badList.push(data.bad);
    }

    return { goodList, normalList, badList };
}
export async function SearchCourse({
  page,
  pageSize,
  type,
  name,
  index,
  teacher
}: {
  page: number
  pageSize: number
  type: number
  name: string
  index: string
  teacher: string
}): Promise<Course[]> {
  const res = (await searchCourse({ page, pageSize, type, name, index, teacher })) as any
  return toMergedCourse(res.data)
}
export async function SelectCourse(id: number): Promise<void> {
  await selectCourse(id)
}
export async function DropCourse(id: number): Promise<void> {
  await dropCourse(id)
}
export async function GetScore(id?: number): Promise<{ score: Score[]; gpa: GPA }> {
  const res = (await getScore(id)) as any
  const score = [] as Score[]
  const s = res.data.score as any[]
  const gpa: GPA = {
    gpa: res.data.GPA.GPA,
    ranking: res.data.GPA.ranking
  }
  s.forEach((val) => {
    score.push({
      index: val.index,
      name: val.name,
      credit: val.credit,
      regular: val.regular,
      finalScore: val.finalScore,
      total: val.total,
      point: val.point,
      ranking: val.ranking
    })
  })
  return { score, gpa }
}
export async function AddComment(id: number, comment: string): Promise<void> {
  await addComment(id, comment)
}
export async function GetAllScore(id: number): Promise<
  [
    {
      number: string
      classNumber: string
      studentName: string
      major: string
      regular: number
      finalScore: number
      userId: number
    }
  ]
> {
  const res = (await getAllScore(id)) as any
  const data = res.data as any[]
  const result = [] as any
  data.forEach((val) => {
    result.push({
      number: val.number,
      classNumber: val.classNumber,
      studentName: val.studentName,
      major: val.major,
      regular: val.regular,
      finalScore: val.finalScore,
      userId: val.userId
    })
  })
  return result
}
export async function UpdateScore(
  userId: number,
  courseId: number,
  regular: number,
  finalScore: number
) {
  await updateScore(userId, courseId, regular, finalScore)
}
export async function UpdateCourse(courseData: any): Promise<void> {
  await updateCourse(courseData)
}
export async function DeleteCourse(id: number): Promise<void> {
  await deleteCourse(id)
}
export async function AddCourse(courseData: any): Promise<void> {
  await addCourse(courseData)
}
export async function UpdateTeacher(courseId: number, teacherIdList: string): Promise<void> {
  await updateTeacher(courseId, teacherIdList)
}
export async function GetCourseScore(id: number): Promise<{ good: number, normal: number, bad: number }> {
    const data = await GetAllScore(id);
    let good: number = 0, normal: number = 0, bad: number = 0;

    data.forEach((item) => {
        if (item.finalScore < 60) {
            bad++;
        } else if (item.finalScore >= 60 && item.finalScore < 90) {
            normal++;
        } else if (item.finalScore >= 90) {
            good++;
        }
    });
    return { good, normal, bad };
}
